import {
  MiddlewareConsumer,
  Module,
  NestModule,
  RequestMethod,
} from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { ServeStaticModule } from "@nestjs/serve-static";
import type { RedisClientOptions } from "redis";
import { CacheModule } from "@nestjs/cache-manager";
import { TypeOrmModule } from "@nestjs/typeorm";
import { AccessMiddleware } from "@/core/middlewares/access.middleware";
import { XMLMiddleware } from "@/core/middlewares/xml.middleware";
import { UserModule } from "@/modules/user/user.module";
import {
  AppService,
  buildTypeormModuleConfig,
  buildCacheModuleConfig,
  buildServeStaticConfig,
} from "@/app.service";
import { SharedModule } from "@/modules/shared/shared.module";
import { ScheduleModule } from "@/modules/schedule/schedule.module";
import { AuthModule } from "@/modules/auth/auth.module";
import { DbModule } from "@/db/db.module";
import { WechatModule } from "@/modules/wechat/wechat.module";
import { ChatModule } from "@/modules/chat/chat.module";
import { NextModule } from "./modules/next/next.module";
import { UeditorModule } from "./modules/ueditor/ueditor.module";
import { FranceModule } from "./modules/france/france.module";

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    TypeOrmModule.forRootAsync(buildTypeormModuleConfig()),
    CacheModule.registerAsync<RedisClientOptions>(buildCacheModuleConfig()),
    ServeStaticModule.forRootAsync(buildServeStaticConfig()),
    DbModule,
    UserModule,
    SharedModule,
    ScheduleModule,
    AuthModule,
    WechatModule,
    ChatModule,
    NextModule,
    UeditorModule,
    FranceModule,
  ],
  providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(...[AccessMiddleware])
      .forRoutes({ path: "*", method: RequestMethod.ALL });

    consumer
      .apply(XMLMiddleware)
      .forRoutes({ path: "wechat/*", method: RequestMethod.POST });
  }
}
